#pragma once

#include <hiredis/async.h>
#include <string>

class HiredisAsyncContext
{
    friend class HiredisSubscriber;

public:
    HiredisAsyncContext(const std::string &ip, uint16_t port);
    ~HiredisAsyncContext();

protected:
    static void connected(const redisAsyncContext *ctx, int status);
    static void disconnected(const redisAsyncContext *ctx, int status);

public:
    bool init();      // 初始化
    bool connect();   // 连接
    void dispatch();  // 阻塞
    void loopbreak(); // 中断 dispatch

protected:
    bool execute(redisCallbackFn *fn, void *privdata, const char *command, ...);

private:
    std::string               m_ipaddr;
    uint16_t                  m_port;
    struct event_base        *m_base;
    struct redisAsyncContext *m_context;
};